package com.server.llh.llhserverwork.activity.work;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.ServerNetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.base.LlhBaseActivity;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.HelpTaskBean;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ZTF on 2017-06-06.
 */

public class HelpActivity extends LlhBaseActivity {
    private static  final String TAG="HelpActivity";
    @BindView(R.id.startTime)
    TextView startTime;
    @BindView(R.id.stopTime)
    TextView stopTime;
    @BindView(R.id.queryServer)
    TextView queryServer;
    @BindView(R.id.timeView)
    LinearLayout timeView;
    @BindView(R.id.health_lv)
    ListView healthLv;
    SwipeRefreshLayout refehsLayout;
    private String waiterId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        refehsLayout = (SwipeRefreshLayout) this
                .findViewById(R.id.swipe_container);
        waiterId= SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_ID,getApplicationContext());
        getTask("", "");
        refehsLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                getTask("", "");
            }
        });
    }
    private void getTask(String beginTime, String endTime) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("waiterId", waiterId);
        lRequest.addParam("beginTime", beginTime);
        lRequest.addParam("endTime", endTime);
        lRequest.setUri(ServerPort.SEARCH_AUTOMA_JSON);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), "服务器繁忙!");
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null) {
                Log.d(TAG,"responseBean=="+responseBean);
                }
            }
        });
    }

    class HelpAdapter extends BaseAdapter {
        List<HelpTaskBean> helpList;
        Context context;
        public	HelpAdapter(Context context,List<HelpTaskBean> helpList){
            this.context=context;
            this.helpList=helpList;
        }
        @Override
        public int getCount() {
            if (helpList==null&&helpList.size()==0) {

                return 0;
            }
            return helpList.size();
        }

        @Override
        public Object getItem(int position) {
            return helpList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            ViewHolder holder = new ViewHolder();
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.subscribe_item_view, null);
                holder.image_clie = (ImageView) convertView
                        .findViewById(R.id.image_clie);
                holder.server_name = (TextView) convertView
                        .findViewById(R.id.client_name);
                holder.client_age = (TextView) convertView
                        .findViewById(R.id.client_age);
                holder.server_time = (TextView) convertView
                        .findViewById(R.id.server_time);
                holder.order_time = (TextView) convertView
                        .findViewById(R.id.order_time);
                holder.client_num = (TextView) convertView
                        .findViewById(R.id.client_num);
                holder.client_server = (TextView) convertView
                        .findViewById(R.id.client_server);
                holder.subscribe_server_order = (TextView) convertView
                        .findViewById(R.id.subscribe_server_order);
                holder.subscribe_Revoke = (TextView) convertView
                        .findViewById(R.id.subscribe_Revoke);
                holder.client_address = (TextView) convertView
                        .findViewById(R.id.client_address);
                holder.textcreate = (TextView) convertView
                        .findViewById(R.id.textcreate);
                holder.ll_value = (LinearLayout) convertView
                        .findViewById(R.id.ll_value);
                holder.ll_value.setVisibility(View.GONE);
                holder.subscribe_Revoke.setVisibility(View.GONE);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final HelpTaskBean helpTask=helpList.get(position);
            holder.image_clie.setBackgroundResource(R.drawable.alarmlog);
            holder.server_name.setText(""+helpTask.getCUSTOMER_NAME());
            holder.client_num.setText(""+helpTask.getCUSTOMER_ADDR());
            holder.client_address.setText(""+helpTask.getCUSTOMER_PHONE());
            holder.client_server.setText(""+helpTask.getSTART_TIME());
            holder.server_time.setText(""+helpTask.getWAITER_NAME());

            holder.subscribe_Revoke.setVisibility(View.GONE);
            if (1==helpTask.getSER_STATUS()) {
                holder.subscribe_server_order.setVisibility(View.GONE);
            }else{
                holder.subscribe_server_order.setText("执行");
                holder.subscribe_server_order.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        int taskId=helpTask.getTRANS_ID();
                        setTask(taskId, 1);
                    }
                });
            }

            return convertView;
        }
        class ViewHolder {
            TextView server_name, server_time, server_finish, order_time,
                    client_server, client_age, client_address, client_num,
                    subscribe_Revoke, subscribe_server_order, textcreate,
                    client_server_time, unitName;
            ImageView llh_image,image_clie;
            LinearLayout ll_value;
        }
        // 将时间戳转为字符串
//				 public String getStrTime(String cc_time) {
//				 String re_StrTime = null;
//				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
//				 long lcc_time = Long.valueOf(cc_time);
//				 re_StrTime = sdf.format(new Date(lcc_time * 1000L));
//				 return re_StrTime;
//				 }
    }
    private void setTask(int taskId,int status) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("taskId", taskId);
        lRequest.addParam("status", status);
        lRequest.setUri(ServerPort.SEARCH_AUTOMA_JSON);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), "服务器繁忙!");
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null) {
                    Log.d(TAG,"responseBean=="+responseBean);
                }
            }
        });
    }

    @Override
    public void showProgress() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return ServerNetRequestManager.getInstance();
    }

    @Override
    public void hideProgress() {

    }

    @Override
    protected int getlayout() {
        return R.layout.activity_health_task;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

}
